<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-row v-if="type == 1">
            <el-col :span="12">
                <el-form-item label="证书名称" prop="title">
                    <el-input v-model="ruleForm.title" />
                </el-form-item>
                <el-form-item label="公司名称" prop="company">
                    <el-input v-model="ruleForm.company" />
                </el-form-item>
                <el-form-item label="资格证书编号" prop="numbers">
                    <el-input v-model="ruleForm.numbers" />
                </el-form-item>
                <el-form-item label="资质类别" prop="aptitude">
                    <el-input v-model="ruleForm.aptitude" />
                </el-form-item>
                <el-form-item label="发证机关" prop="licence">
                    <el-input v-model="ruleForm.licence" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="资格取得时间" prop="acquired_at">
                    <el-date-picker v-model="ruleForm.acquired_at" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="有效期起" prop="start_date">
                    <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="有效期" prop="date_type">
                    <el-radio-group v-model="ruleForm.date_type">
                        <el-radio :value="1" size="small">长期</el-radio>
                        <el-radio :value="0" size="small">时限</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="ruleForm.date_type == 0" label="失效日期" prop="end_date">
                    <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 4 }" />
                </el-form-item>
                <el-form-item label="证书文件" prop="files">
                    <el-button type="primary" size="small" @click="showUpload = true"> 上传 </el-button>
                    <el-button :type="ruleForm.files ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.files)"> 查看 </el-button>
                </el-form-item>
                
                
            </el-col>
        </el-row>
<!-- —————————————————————————————————————————————————————————————————————————————————————————————————————————————————— -->
        <el-row v-if="type == 2">
            <el-col :span="12">
                <el-form-item label="证书类别" prop="title">
                    <el-input v-model="ruleForm.title" />
                </el-form-item>
                <el-form-item label="证书编号" prop="numbers">
                    <el-input v-model="ruleForm.numbers" />
                </el-form-item>
                <!-- <el-form-item label="证书类别" prop="aptitude">
                    <el-input v-model="ruleForm.aptitude" />
                </el-form-item> -->
                <el-form-item label="专业" prop="major">
                    <el-input v-model="ruleForm.major" />
                </el-form-item>
                <el-form-item label="资格等级" prop="level">
                    <el-select v-model="ruleForm.level" placeholder="请选择">
                        <el-option label="" value="" />
                        <el-option label="一级" value="一级" />
                        <el-option label="二级" value="二级" />
                        <el-option label="三级" value="三级" />
                        <el-option label="四级" value="四级" />
                        <el-option label="五级" value="五级" />
                    </el-select>
                </el-form-item>
                <el-form-item label="资格取得时间" prop="acquired_at">
                    <el-date-picker v-model="ruleForm.acquired_at" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="批准单位" prop="approving_unit">
                    <el-input v-model="ruleForm.approving_unit" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="有效期起" prop="start_date">
                    <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="有效期至" prop="date_type">
                    <el-radio-group v-model="ruleForm.date_type">
                        <el-radio :value="1" size="small">长期</el-radio>
                        <el-radio :value="0" size="small">时限</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="ruleForm.date_type == 0" label="失效日期" prop="end_date">
                    <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 4 }" />
                </el-form-item>
                <el-form-item label="证书文件" prop="files">
                    <el-button type="primary" size="small" @click="showUpload = true"> 上传 </el-button>
                    <el-button :type="ruleForm.files ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.files)"> 查看 </el-button>
                </el-form-item>
                
            </el-col>
        </el-row>

<!-- —————————————————————————————————————————————————————————————————————————————————————————————————————————————————— -->
        <el-row v-if="type == 3">
            <el-col :span="12">
                <el-form-item label="证书名称" prop="title">
                    <el-input v-model="ruleForm.title" />
                </el-form-item>
                <el-form-item label="资格证书编号" prop="numbers">
                    <el-input v-model="ruleForm.numbers" />
                </el-form-item>
                <el-form-item label="专业" prop="major">
                    <el-input v-model="ruleForm.major" />
                </el-form-item>
                <el-form-item label="资格等级" prop="level">
                    <el-select v-model="ruleForm.level" placeholder="请选择">
                        <el-option label="" value="" />
                        <el-option label="初级" value="初级" />
                        <el-option label="中级" value="中级" />
                        <el-option label="高级" value="高级" />
                        <el-option label="正高级" value="正高级" />
                    </el-select>
                </el-form-item>
                <el-form-item label="资格取得时间" prop="acquired_at">
                    <el-date-picker v-model="ruleForm.acquired_at" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="批准单位" prop="approving_unit">
                    <el-input v-model="ruleForm.approving_unit" />
                </el-form-item>
                <el-form-item label="评委会名称" prop="jury_name">
                    <el-input v-model="ruleForm.jury_name" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="生效日期" prop="start_date">
                    <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="有效期" prop="date_type">
                    <el-radio-group v-model="ruleForm.date_type">
                        <el-radio :value="1" size="small">长期</el-radio>
                        <el-radio :value="0" size="small">时限</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="ruleForm.date_type == 0" label="失效日期" prop="end_date">
                    <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 4 }" />
                </el-form-item>
                <el-form-item label="证书文件" prop="files">
                    <el-button type="primary" size="small" @click="showUpload = true"> 上传 </el-button>
                    <el-button :type="ruleForm.files ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.files)"> 查看 </el-button>
                </el-form-item>
            </el-col>
        </el-row>

<!-- —————————————————————————————————————————————————————————————————————————————————————————————————————————————————— -->
        <el-row v-if="type == 4">
            <el-col :span="12">
                <el-form-item label="专利名称" prop="title">
                    <el-input v-model="ruleForm.title" />
                </el-form-item>
                <el-form-item label="专利类型" prop="aptitude">
                    <el-input v-model="ruleForm.aptitude" />
                </el-form-item>
                <el-form-item label="专利号" prop="numbers">
                    <el-input v-model="ruleForm.numbers" />
                </el-form-item>
                <el-form-item label="国别" prop="nationality">
                    <el-input v-model="ruleForm.nationality" />
                </el-form-item>
                <el-form-item label="发明人" prop="inventor">
                    <el-input v-model="ruleForm.inventor" />
                </el-form-item>
                <el-form-item label="专利权所有人" prop="patent_owner">
                    <el-input v-model="ruleForm.patent_owner" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="授予时间" prop="acquired_at">
                    <el-date-picker v-model="ruleForm.acquired_at" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="生效日期" prop="start_date">
                    <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="有效期" prop="date_type">
                    <el-radio-group v-model="ruleForm.date_type">
                        <el-radio :value="1" size="small">长期</el-radio>
                        <el-radio :value="0" size="small">时限</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="ruleForm.date_type == 0" label="失效日期" prop="end_date">
                    <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 4 }" />
                </el-form-item>
                <el-form-item label="证书文件" prop="files">
                    <el-button type="primary" size="small" @click="showUpload = true"> 上传 </el-button>
                    <el-button :type="ruleForm.files ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.files)"> 查看 </el-button>
                </el-form-item>
            </el-col>
        </el-row>
        
    </el-form>
    <div class="mt20 text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
    <el-dialog v-model="showUpload" title="文件">
        <component :is="uploadFile" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

    <el-dialog v-model="isShowFile" title="文件" width="80%" @close="onCloseFile">
        <component :is="showFile" :data="files"/>
    </el-dialog>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
import uploadFile from '@/components/uploadFile.vue'
import showFile from '@/components/showFile.vue'
const files = ref<string>('')
const isShowFile = ref<boolean>(false)
const loading = ref<boolean>(false)
const showUpload = ref<boolean>(false)
let uploadData:any = ref({prefix:'cert/'})
interface RuleForm {
    id: number|string
    user_id:number
    title: string
    company:string
    numbers: string
    level:string
    major:string
    acquired_at:string
    approving_unit:string
    jury_name:string
    type: number
    start_date: string
    end_date: string
    files: string
    date_type:number
    aptitude:string
    inventor:string
    patent_owner:string
    nationality:string
    licence:string
    remarks:string
} 

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    user_id:0,
    title: '',
    company:'/',
    numbers:'',
    level:'',
    major:'/',
    acquired_at:'',
    approving_unit:'/',
    jury_name:'/',
    files: '',
    type: 1,
    start_date: '',
    end_date: '',
    date_type:1,
    aptitude:'/',
    inventor:'/',
    patent_owner:'/',
    nationality:'/',
    licence:'',
    remarks:'/'
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/cert/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const props = defineProps<{
    user_id:number
    id:number
    type:number
}>()

watch(props,()=>{
    ruleForm.value.id = props.id
    ruleForm.value.user_id = props.user_id
    ruleForm.value.title = ''
    ruleForm.value.company = ''
    ruleForm.value.numbers = ''
    ruleForm.value.files = ''
    ruleForm.value.start_date = ''
    ruleForm.value.end_date = ''
    ruleForm.value.major = ''
    ruleForm.value.acquired_at = ''
    ruleForm.value.approving_unit = ''
    ruleForm.value.jury_name = '/'
    ruleForm.value.aptitude = '/'
    ruleForm.value.inventor = '/'
    ruleForm.value.patent_owner = '/'
    ruleForm.value.nationality = '/'
    ruleForm.value.date_type = 1
    ruleForm.value.type = props.type
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
    ],
    numbers: [
        { required: true, message: '请输入编号', trigger: 'blur' },
    ],
    start_date: [
        { required: true, message: '请输入生效日期', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/cert',ruleForm.value)
            }else{
                request = http.put('/cert',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);return
                }
                ElMessage.success(response.data.msg);
                onSave()  
            })
                      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])

const saveUpload = (data:string) => {
    ruleForm.value.files = data
    showUpload.value = false
}

const onShowFile = (file:string) => {
    files.value = file
    isShowFile.value = true
}
const onCloseFile = () => {
    files.value = ''
}
</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
